﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace Jy123.Web.Module
{
    public partial class RaceResult : System.Web.UI.Page
    {
        private string[] strHeaderCss = { "", "", "NavIcon", "" };
        protected void Page_Load(object sender, EventArgs e)
        {
            string strHtml = Cache["raceresult"].ToString();
            string strRUl = Cache["raceresultul"].ToString();
            BLL.Race.Race BLLRace = new Jy123.BLL.Race.Race();
            BLL.Venues_Main BLLVenues = new Jy123.BLL.Venues_Main();
            string strVId = Request.QueryString["VenuesId"];
            Model.Venues_Main venues = BLLVenues.GetModel(int.Parse(strVId));
            BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
            Model.Public_JType JType = BLLJType.GetModel(venues.FJType.Value);
            bool isCar = (JType.FSportType.Value == Convert.ToInt32(BLL.Public.EnumConfig.SportType.Car) || JType.FSportType.Value == Convert.ToInt32(BLL.Public.EnumConfig.SportType.F1));
            Jy123.Web.AppCode.Module appModule = new Jy123.Web.AppCode.Module();
            string strHeader = Cache["header"].ToString();
            string headerString = appModule.BindHeader(strHeader, strHeaderCss);
            string strMain = Cache["mainmenu"].ToString();
            string strDiv = Cache["mainmenusubdiv"].ToString();
            string strLi = Cache["mainmenusubdivli"].ToString();
            int cUserId = int.Parse(Common.Cookie.getCookie("UserID"));
            string mainMenuString = appModule.BindMainMenu(strMain, strDiv, strLi, cUserId);

            strHtml = strHtml.Replace("$Header$", headerString);
            strHtml = strHtml.Replace("$MainMenu$", mainMenuString);
            strHtml = strHtml.Replace("$Footer$", Cache["footer"].ToString());
            string strBlock = "display:block;";
            string strNone = "display:none;";
            ArrayList resultArrayList;
            StringBuilder sbString = new StringBuilder();
            if (isCar)
            {
                resultArrayList = BLLRace.EndCarRace(venues);
                strHtml = strHtml.Replace("$ResultName$", "名次");
                strHtml = strHtml.Replace("$scorestyle$", strNone);
                strHtml = strHtml.Replace("$Score$", string.Empty);
                for (int i = 0; i < resultArrayList.Count; i++)
                {
                    BLL.Race.Race.ClubRank cRank = (BLL.Race.Race.ClubRank)resultArrayList[i];
                    string cCUl = strRUl;
                    cCUl = cCUl.Replace("$ClubName$", cRank.ClubName);
                    cCUl = cCUl.Replace("$RaceCost$", cRank.Cost.ToString());
                    cCUl = cCUl.Replace("$Premium$", cRank.Prelium.ToString());
                    cCUl = cCUl.Replace("$Rank$", (i + 1).ToString());
                    sbString.Append(cCUl);
                }
            }
            else
            {
                string[] strCFClub = venues.RaceClub.Split(',');
                resultArrayList = BLLRace.EndRace(venues);
                strHtml = strHtml.Replace("$ResultName$", "比分");
                strHtml = strHtml.Replace("$scorestyle$", strBlock);
                strHtml = strHtml.Replace("$Score$", resultArrayList[1].ToString());
                BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
                Model.Public_Level level = BLLLevel.GetModel(venues.FLevel.Value);                
                bool isFirstWin = strCFClub[0].Equals(resultArrayList[0].ToString());
                BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
                for (int i = 0; i < 2; i++)
                {
                    Model.Club_Main club = BLLClub.GetModel(int.Parse(strCFClub[i].ToString()));
                    string cFUl = strRUl;
                    cFUl = cFUl.Replace("$ClubName$", club.CName);
                    cFUl = cFUl.Replace("$RaceCost$", (Convert.ToInt32(level.Cost.Value)).ToString());
                    if (isFirstWin && i == 0 || (!isFirstWin && i == 1))
                    {
                        cFUl = cFUl.Replace("$Premium$", (Convert.ToInt32(level.Premium.Value)).ToString());
                        cFUl = cFUl.Replace("$Rank$", resultArrayList[2].ToString());
                    }
                    else
                    {
                        cFUl = cFUl.Replace("$Premium$", "0");
                        cFUl = cFUl.Replace("$Rank$", resultArrayList[3].ToString());
                    }
                    sbString.Append(cFUl);
                }
            }
            strHtml = strHtml.Replace("$Ul$", sbString.ToString());
            Response.Write(strHtml);
        }
    }
}
